#include "StdAfx.h"
#include "GifShowEx.h"


CGifShowEx::CGifShowEx(void)
{
	m_pImage = NULL;
	m_pDimensionIDs = NULL;
	m_pItem = NULL;
}


CGifShowEx::~CGifShowEx(void)
{
}

int CGifShowEx::Load(LPCWSTR pszFileName, int nImageCount)
{
	m_pImage = new Gdiplus::Image(pszFileName);
	
	//First of all we should get the number of frame dimensions
	//Images considered by GDI+ as:
	//frames[animation_frame_index][how_many_animation];
	UINT count = m_pImage->GetFrameDimensionsCount();

	//Now we should get the identifiers for the frame dimensions 
	m_pDimensionIDs =new GUID[count];
	m_pImage->GetFrameDimensionsList(m_pDimensionIDs, count);
	
	//For gif image , we only care about animation set#0
	WCHAR strGuid[39];
	StringFromGUID2(m_pDimensionIDs[0], strGuid, 39);
	m_FrameCount = m_pImage->GetFrameCount(&m_pDimensionIDs[0]);

	//PropertyTagFrameDelay is a pre-defined identifier 
	//to present frame-delays by GDI+
	UINT TotalBuffer = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay);
	m_pItem = (Gdiplus::PropertyItem*)malloc(TotalBuffer);
	m_pImage->GetPropertyItem(PropertyTagFrameDelay,TotalBuffer,m_pItem);

	//image = new Image(pszFileName);
	//count = image->GetFrameDimensionsCount();
	//GUID *pDimensionIDs=(GUID*)new GUID[count];
	//image->GetFrameDimensionsList(pDimensionIDs, count);
	//WCHAR strGuid[39];
	//StringFromGUID2(pDimensionIDs[0], strGuid, 39);
	//UINT frameCount=image->GetFrameCount(&pDimensionIDs[0]);
	//delete []pDimensionIDs;
	return 0;
}

int CGifShowEx::Draw(HDC dc, int nImage, int destx, int desty
						, DWORD drawStyle, float alpha)
{
	Gdiplus::Graphics g(dc);
	g.DrawImage(m_pImage,0,0,m_pImage->GetWidth(),m_pImage->GetHeight());

	return 0;
}



void CGifShowEx::Play()
{
	m_iCurrentFrame = 0;
	GUID Guid = Gdiplus::FrameDimensionTime;
	m_pImage->SelectActiveFrame(&Guid,m_iCurrentFrame);


	++ m_iCurrentFrame;
}

void CGifShowEx::NextPlay()
{
	GUID Guid = Gdiplus::FrameDimensionTime;
	m_pImage->SelectActiveFrame(&Guid,m_iCurrentFrame);
	m_iCurrentFrame = (++ m_iCurrentFrame) % m_FrameCount;
}

